import { Component, Host, OnInit, ChangeDetectorRef } from '@angular/core';
import { KnowledgeCenterService } from './knowledge-center.service';
import { InsightModel } from './models/insight.model';
import { NewsBaseModel } from './models/news.model';
import { EventModel } from './models/event.model';
import { AppComponent } from '../../app.component';
import { ActivatedRoute } from '@angular/router';
import { BaseComponent } from '../../shared/components/baseComponent/base.component';
import { LrtmService } from '../../framework/lrtm/lrtm.service';
import { AnalyticsService } from '../../shared/services/googleAnalytics.service';
import { GoogleAnalyticsBeacon } from '../../shared/models/googleAnalytics.model';

@Component({
    templateUrl: './knowledge-center.component.html',
    styleUrls: ['./knowledge-center.component.scss'],
    providers: [KnowledgeCenterService, LrtmService]
})
export class KnowledgeCenterComponent extends BaseComponent implements OnInit {

    public spotlights: NewsBaseModel[] = [];
    public events: EventModel[] = [];
    public insights: InsightModel[] = [];
    public section: string;
    public id: string;
    public article = undefined;

    constructor(
        @Host() private rootComponent: AppComponent,
        private route: ActivatedRoute,
        private kcService: KnowledgeCenterService,        
        private cdRef: ChangeDetectorRef,
        private analyticsService: AnalyticsService
    ) {
        super(cdRef);
    }

    ngOnInit() {
        this.route.params.subscribe(params => {
            window.scrollTo(0, 0);
            this.spotlights = this.events = this.insights = undefined;
            this.section = params['type'];
            this.id = params['id'];

            switch (this.section) {
                case 'spotlight':
                    this.getSpotlights();
                    break;

                case 'events':
                    this.getEvents();
                    break;

                case 'news':
                default:
                    this.getInsights();
                    break;
            }

            this.cdRef.markForCheck();
            this.rootComponent.finishLoading();
        });
    }

    private getSpotlights(): void {
        this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('Spotlight', 'Knowledge Center', {}));
        this.kcService.getSpotlight(false).subscribe(data => {
            this.spotlights = data;
        });
    }

    private getEvents(): void {
        this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('Events', 'Knowledge Center', {}));
        this.kcService.getEvents(false, true).subscribe(data => {
            this.events = data.items;
        });
    }

    private getInsights(): void {
        this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('News', 'Knowledge Center', {}));
        this.kcService.getKnowledge(false, true).subscribe(data => {
            this.insights = data;
        });
    }
}
